
<template>
  <el-dialog v-model="ifShow" :title="modelTitle" width="70rem" :before-close="handleCancel">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="auto" class="demo-ruleForm" status-icon>
      <el-form-item label="平台" prop="platformId">
        <el-select multiple clearable v-model="formData.platformId" placeholder="请选择">
          <el-option v-for="item in platFormArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="渠道" prop="channelId">
        <el-select multiple clearable v-model="formData.channelId" placeholder="请选择">
          <el-option v-for="item in channelArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型" prop="bizType">
        <el-select multiple clearable v-model="formData.bizType" placeholder="请选择">
          <el-option v-for="item in bizTypeArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="城市" prop="cityId">
        <el-select multiple filterable clearable v-model="formData.cityId" placeholder="请选择" @change="handleCityChange">
          <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="车型" prop="vehicleTypeId">
        <el-select multiple filterable clearable v-model="formData.vehicleTypeId" placeholder="请选择">
          <el-option v-for="item in MycarTypeArr" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商配置" prop="supplierConfig">
        <el-radio-group v-model="formData.supplierConfig.type">
          <el-radio :value="1">合计</el-radio>
          <el-radio :value="2">分佣</el-radio>
        </el-radio-group>
        <el-input-number style="margin: 0 14px" v-model="formData.supplierConfig.percent"></el-input-number>
        <el-input style="width: 14rem" placeholder="结算起步价" v-model="formData.supplierConfig.start">
          <template #prepend>≥</template>
          <template #append>元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="司机配置">
        <div class="flex_box" v-for="(item, i) in staArr" :key="item.id">
          <el-input style="margin: 0 14px; width: 210px" placeholder="请输入司机比例" v-model="item.driverRate" suffix="%">
            <template #append>%</template>
          </el-input>
          <el-input style="margin: 0 14px; width: 210px" placeholder="请输入价格" v-model="item.price" suffix="元" prefix="<">
            <template #prepend>
              <el-icon><ArrowLeft /></el-icon>
            </template>
            <template #append>元</template>
          </el-input>
          <el-button type="danger" @click="handleRemove('sta', i)">删除</el-button>
          <el-button style="position: absolute; right: -80px" v-if="i == staArr.length - 1" type="primary" @click="handleAdd('sta')">新增</el-button>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleOk">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import CommonApi from "@/api/common";
import JntApi from "@/api/jnt";
import Supplier from "@/api/supplier";
import { ref } from "vue";
import { channelArr, bizTypeArr, orderTypeArr, platFormArr, carTypeArr } from "@/enums/common";

const props = defineProps<{}>();
const emit = defineEmits(["success"]);
const formRef: any = ref(null);
let formData: any = ref({
  id: "",
  platformId: [],
  channelId: [],
  bizType: undefined,
  cityId: undefined,
  vehicleTypeId: undefined,
  luggageType: undefined,
  stair: "",
  supplierConfig: {
    type: 1,
    start: 0,
    percent: null,
  },
});
const rules = reactive({
  bizType: [{ required: true, message: "请输入类型" }],
  platformId: [{ required: true, message: "请输入平台" }],
  channelId: [{ required: true, message: "请输入渠道" }],
  cityId: [{ required: true, message: "请选择城市!" }],
  isOnline: [{ required: true, message: "请输入是否上线!" }],
});
const staArr: any = ref([
  {
    driverRate: "",
    price: "",
    id: Date.now() + Math.random().toString(16).slice(2),
  },
]); //里程价
const polyArr: any = ref([{ supplierId: "", percent: "1", mathNum: Date.now() + Math.random().toString(16).slice(2) }]); //里程价
const ifShow = ref(false);
const modelTitle = ref();
const cityOptions = ref();
const MycarTypeArr = ref<{ label: string; value: number }[]>([]);
function edit(title: any, row: any) {
  MycarTypeArr.value = JSON.parse(JSON.stringify(carTypeArr));
  MycarTypeArr.value.unshift({ label: "兜底车型", value: 0 });
  modelTitle.value = title;
  ifShow.value = true;

  if (row && row.id) {
    // 里程价格
    let { stair } = row;

    if (stair.length) {
      staArr.value = JSON.parse(JSON.stringify(stair));
    }
  }

  if (!row.supplierConfig) {
    row.supplierConfig = {
      type: 1,
      percent: null,
      start: 0,
    };
  }
  if (title == "新增") {
    formData.value = {
      id: "",
      platformId: [],
      channelId: [],
      bizType: undefined,
      cityId: undefined,
      vehicleTypeId: undefined,
      luggageType: undefined,
      stair: "",
      supplierConfig: {
        type: 1,
        start: 0,
        percent: null,
      },
    };
  }
  //赋值
  Object.assign(formData.value, row);
  if (title == "编辑") {
    formData.value.channelId = [row.channelId];
    formData.value.bizType = [row.bizType];
    formData.value.platformId = [row.platformId];
    formData.value.vehicleTypeId = [row.vehicleTypeId];
    formData.value.cityId = [row.cityId];
  }
}

function handleAdd(code: any) {
  if (code == "sta") {
    staArr.value.push({
      driverRate: "",
      price: "",
      id: Date.now() + Math.random().toString(16).slice(2),
    });
  }
}

function handleRemove(code: any, index: any) {
  if (code == "sta") {
    if (staArr.value.length !== 1) {
      staArr.value.splice(index, 1);
    }
  }
}
/**
 * 确定按钮点击事件
 */
async function handleOk() {
  await formRef.value.validate((valid: any, fields: any) => {
    if (valid) {
      let model: any = formData.value;
      model.stair = staArr.value;
      JntApi.commissionEdit(model).then((res: any) => {
        if (res.code == 0) {
          ElMessage.success(res.message);
          formRef.value.resetFields();
          emit("success");
          handleCancel();
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
}

//城市下拉框点击事件
function handleCityChange(value: any) {
  formData.value.cityName = cityOptions.value.find((item: any) => item.id == value).name;
}

/**
 * 取消按钮回调事件
 */
function handleCancel() {
  formRef.value.resetFields();
  staArr.value = [
    {
      driverRate: "",
      price: "",
      id: Date.now() + Math.random().toString(16).slice(2),
    },
  ];
  ifShow.value = false;
}

defineExpose({
  edit,
});
onMounted(() => {
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data: any) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.flex_box {
  display: flex;
  margin-bottom: 14px;
  position: relative;
}
</style>
